//
//  SZHomeViewController.m
//  03-仿网易新闻
//
//  Created by ItHeiMa on 2016/12/31.
//  Copyright © 2016年 itHeima. All rights reserved.
//

#import "SZHomeViewController.h"
#import "SZChannelView.h"
#import "SZChannelModel.h"
#import "SZNewsListViewController.h"

@interface SZHomeViewController ()<UIPageViewControllerDataSource, UIPageViewControllerDelegate, SZChannelViewDelegate> {
    //频道控制
    SZChannelView *_clView;
    UIPageViewController *_pageViewController;
}

@end

@implementation SZHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //在navigationController中， 会自动调整scrollView的contentInset
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //设置UI
    [self setupUI];
}

/**
 设置UI
 */
- (void) setupUI {
    [self setupChannelView];
    [self setupPageViewController];
}

/**
 设置频道的控件
 */
- (void) setupChannelView {
    _clView = [[SZChannelView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, 40)];
    _clView.currentIndex = 0;
    _clView.delegate = self;
    [self.view addSubview:_clView];
    _clView.channelList = [SZChannelModel channelList];
}

/**
 设置pageViewController
 */
- (void) setupPageViewController {
    //1. 初始化pageViewController
    _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    //2. 添加pageViewController
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [_pageViewController didMoveToParentViewController:self];
    
    [_pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_clView.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    //3. 设置子控制器
    SZChannelModel *model = [_clView.channelList objectAtIndex:0];
    SZNewsListViewController *newsList = [[SZNewsListViewController alloc] initWithIndex:0 model:model];
    [_pageViewController setViewControllers:@[newsList] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    //4. 设置并实现数据源与代理
    _pageViewController.dataSource = self;
    _pageViewController.delegate = self;
}

#pragma mark - UIPageViewController 的数据源 

/**
 返回前一个控制器

 @param viewController viewController 当前显示的控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(SZNewsListViewController *)viewController {
    //判断当前子控制器是否第一个，如果是不再创建
    NSInteger index = viewController.index;
    if (index == 0) return nil;
    
    //创建当前控制器的前一个控制器
    SZChannelModel *model = [_clView.channelList objectAtIndex: index-1];
    SZNewsListViewController *newsList = [[SZNewsListViewController alloc] initWithIndex:index-1 model:model];
    
    return newsList;
}

/**
 返回后一个控制器
 
 @param viewController viewController 当前显示的控制器
 */
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(SZNewsListViewController *)viewController {
    //判断当前控制器是否是最后一个，如果是不再创建
    NSInteger index = viewController.index;
    if (index == _clView.channelList.count - 1) return nil;
    
    //创建后一个控制器
    SZChannelModel *model = [_clView.channelList objectAtIndex: index+1];
    SZNewsListViewController *newsList = [[SZNewsListViewController alloc] initWithIndex:index+1 model:model];
    return newsList;
}

#pragma mark - UIPageViewController Delegate
/**
 将要发生动画时调用的方法

 @param pageViewController 当前的pageViewController
 @param pendingViewControllers 将要展示的UIViewController的数组（只有一个元素）
 */
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    //将要展示的子控制器
    SZNewsListViewController *next = (SZNewsListViewController *)pendingViewControllers[0];
    //NSLog(@"将要展示的ViewController：%ld， %@", next.index, next.model.tname);

    //动画完成时调用的方法, 有时候可能不会调用，所以在这个地方再调用一下；
    [_clView setCurrentIndex:next.index];
}

/**
 动画完成时调用的方法

 @param pageViewController 当前的pageViewController
 @param previousViewControllers 前一个UIViewController的数组（只有一个元素）
 */
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    //动画完成之后，当前显示的子控制器
    SZNewsListViewController *current = (SZNewsListViewController *)pageViewController.viewControllers[0];
    //NSLog(@"finish：当前的ViewController：%ld， %@", current.index, current.model.tname);
    
    //实现pageViewController与频道控件的联动
    [_clView setCurrentIndex:current.index];
}

#pragma mark - SZChannelViewDelegate

/**
 点击ChannelView的lable，切换频道

 @param index 将要切换的频道的index
 */
- (void) channelChanged:(NSInteger)index {
    //1. 确定当前控制器的index 和 index的大小
    //1>a 取到当前控制器的index
    SZNewsListViewController *current = (SZNewsListViewController *)_pageViewController.viewControllers[0];
    NSInteger currentIndex = current.index;
    
    //如果是同一个index, 直接返回
    if (currentIndex == index) return;
    
    //默认是从右往左滑
    UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;
    
    //如果index小于当前子控制器的index，则从左往右滑
    if (index < currentIndex) {
        direction = UIPageViewControllerNavigationDirectionReverse;
    }
    
    //2. 重新设置子控制器
    SZChannelModel *model = [_clView.channelList objectAtIndex:index];
    SZNewsListViewController *newsList = [[SZNewsListViewController alloc] initWithIndex:index model:model];
    
    //UIPageViewControllerNavigationDirectionForward: 从左往右滑，还是从右往滑
    [_pageViewController setViewControllers:@[newsList] direction:direction animated:YES completion:nil];
}

@end






